package com.zhongwang.mms.module.buy.service;

import com.zhongwang.mms.gen.entity.BReportPlan;
import com.zhongwang.mms.gen.entity.TDictionary;
import com.zhongwang.mms.gen.entity.VwPdmRepResult;
import com.zhongwang.mms.gen.mapper.BReportPlanMapper;
import com.zhongwang.mms.module.buy.dao.ReportPlanDao;
import com.zhongwang.mms.module.buy.dao.VwPdmRepResultDao;
import com.zhongwang.mms.module.buy.model.ReportPlanModel;
import com.zhongwang.mms.module.system.log.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 计划删除审核 Service
 *
 * @Author: China.sgl
 * @Date: 2020/4/7 16:25
 */
@SuppressWarnings("ALL")
@Service
public class PlanDelCheckService {

    @Autowired
    private LogService logService;
    @Autowired
    private ReportPlanDao reportPlanDao;
    @Autowired
    private BReportPlanMapper bReportPlanMapper;
    @Autowired
    private VwPdmRepResultDao vwPdmRepResultDao;

    @Value("${user.upload.path}")
    private String uploadPath;

    private String msg = "";

    /**
     * 按条件查询提报计划
     *
     * @param reportPlanModel 条件
     * @return list
     */
    public List<ReportPlanModel> findReportPlanByQuery(ReportPlanModel reportPlanModel) {
//        reportPlanModel.setRppStatus(1);
        return reportPlanDao.findReportPlanByQueryWithDelCheck(reportPlanModel);
    }

    /**
     * 根据ID修改提报计划状态并保存日志
     *
     * @param id 主键
     * @param status 状态
     * @param name 日志操作名称
     * @return 行数
     */
    private int updateReportPlanStatus(Integer id, Integer status, String name) {
        ReportPlanModel plan = findReportPlanById(id);
        logService.saveLog(name, "项目名：" + plan.getRppProjectName() + "<br>物料描述：" + plan.getRppMatDesc());
        return reportPlanDao.updateStatusById(id, status);
    }

    /**
     * 根据ID删除提报计划并保存日志
     *
     * @param id 主键
     * @param name 日志操作名称
     * @return 行数
     */
    @Transactional(rollbackFor = Exception.class)
    private int delReportPlan(Integer id, String name) {
        ReportPlanModel plan = findReportPlanById(id);
        if(plan!=null){
            logService.saveLog(name, "项目名：" + plan.getRppProjectName() + "<br>物料描述：" + plan.getRppMatDesc());
        }
        BReportPlan updateRppSatatus = new BReportPlan();
        updateRppSatatus.setRppId(id);
        updateRppSatatus.setRppStatus(-3);
        if(plan.getRppResultId()!=null){
            VwPdmRepResult vwPdmRepResult = vwPdmRepResultDao.getVwPdmRepResultById(plan.getRppResultId());
            if(vwPdmRepResult!=null){
                vwPdmRepResultDao.clearReceiveMark(vwPdmRepResult.getId());
            }
        }
        return reportPlanDao.updateBReportPlanToDelete(updateRppSatatus);
    }

    /**
     * 删除提报计划, 这里真删除
     *
     * @param id 主键
     * @return 行数
     */
    public int deleteById(Integer id) {
        return delReportPlan(id, "删除采购计划");
    }

    /**
     * 批量删除提报计划
     *
     * @param rppIds 提报计划ID数组
     * @return 行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteInId(Integer[] rppIds) {
        int count = 0;
        // 保存日志，生成采购计划
        for (Integer rppId : rppIds) {
            count += deleteById(rppId);
        }
        return count == rppIds.length ? 1 : 0;
    }

    /**
     * 驳回删除提报计划
     *
     * @param id 主键
     * @return 行数
     */
    public int cancelById(Integer id) {
        return updateReportPlanStatus(id, 2, "驳回删除提报计划");
    }

    /**
     * 批量驳回删除提报计划
     *
     * @param rppIds 提报计划ID数组
     * @return 行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int cancelInId(Integer[] rppIds) {
        int count = 0;
        // 保存日志，生成采购计划
        for (Integer rppId : rppIds) {
            count += cancelById(rppId);
        }
        return count == rppIds.length ? 1 : 0;
    }

    /**
     * 根据主键查询提报计划
     *
     * @param id 主键
     * @return ReportPlanModel
     */
    public ReportPlanModel findReportPlanById(Integer id) {
        return reportPlanDao.findReportPlanById(id);
    }

    /**
     * 查询分类列表
     *
     * @return list
     */
    public List<TDictionary> findTypes() {
        return reportPlanDao.findDictionaryByTypeAndStatus(1000, 1);
    }

}
